//
//  SMGuidePage.m
//
 
//

#define kAppDelegate   ((AppDelegate *)[[UIApplication sharedApplication] delegate])

#import "FLYDPage.h"
#import "GLYDTupianView.h"
#import <Masonry/Masonry.h>
#import "GanLuHongFile.h"
#import "AppDelegate.h"

@interface FLYDPage () <UIScrollViewDelegate> {
    NSArray *_imageList;
    NSArray *_titleImageList;
    UIButton *begin;
    UIImageView *_lastImage;
}

@property (strong, nonatomic) UIScrollView *scrollView;

@end

@implementation FLYDPage


- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"glopenGuide"];
    [self initializeDataSource];
    [self initializeAppearance];
}

- (void)initializeDataSource {
    
    _imageList = @[@"new1",@"new2",@"new3"];
    _titleImageList = @[@"newTitle1",@"newTitle2",@"newTitle3"];
}

- (void)initializeAppearance {
    
    UIImageView *img = [[UIImageView alloc] init];
    img.contentMode = UIViewContentModeScaleToFill;
    [self.view addSubview:img];
    img.image = [UIImage imageNamed:@"guaideBg"];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    // 初始化图片到scroll
    for (int i = 0; i < _imageList.count; i ++) {
        GLYDTupianView *image = [GLYDTupianView defaultClassNameNibViewWithFrame:CGRectMake(0 + self.view.bounds.size.width * i, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        image.contentMode = UIViewContentModeScaleAspectFill;
        image.textImg.image = [UIImage imageNamed:_titleImageList[i]];
        image.phoneImg.image = [UIImage imageNamed:_imageList[i]];
        [self.scrollView addSubview:image];
        
    }
    [self initializeBeginButtonOnView];
}


- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * _imageList.count, self.view.bounds.size.height);
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        self.scrollView.delegate = self;
    }
    return _scrollView;
}

// 初始化开始体验按钮
- (void)initializeBeginButtonOnView {
    
    begin = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImageView *btnImg = [[UIImageView alloc] init];
    begin.frame = CGRectMake(0, 0, 290, 78);
    begin.clipsToBounds = YES;
    begin.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height * 0.9);
    [begin addTarget:self action:@selector(beginToExperience) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnImg];
    [self.view addSubview:begin];
    
    
    btnImg.image = [UIImage imageNamed:@"jixuAnniu"];
    btnImg.contentMode = UIViewContentModeScaleAspectFill;
    btnImg.frame = CGRectMake(0, 0, 290, 58);
    btnImg.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height * 0.9);
    
}

/**
 *  开始体验 进入app
 */
- (void)beginToExperience {
    if (self.scrollView.contentOffset.x >= (KScreenWidth * 2.0)) {
        [kAppDelegate toMain];
    } else {
        if (self.scrollView.contentOffset.x == 0) {
            [self.scrollView setContentOffset:CGPointMake(KScreenWidth, 0) animated:YES];
        } else {
            [self.scrollView setContentOffset:CGPointMake(KScreenWidth * 2, 0) animated:YES];
        }
    }
}


@end
